package cn.rongdao.jrkabao.ui.deposit.bill;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.deposit.BillBean;
import cn.rongdao.jrkabao.bean.deposit.SelectMonthBean;
import cn.rongdao.jrkabao.model.deposit.GetBillModel;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashAdvanceTimeUIComponent;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.SelectCardUIComponent;
import cn.rongdao.jrkabao.ui.deposit.popup.SelectMonthPopupUtil;
import cn.rongdao.jrkabao.util.NumberUtil;

/**
 * Created by jack on 2017/7/29
 */

public class MonthBillActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, MonthBillActivity.class);
    }

    @BindView(R.id.amb_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.amb_cash_advance_time)
    CashAdvanceTimeUIComponent cashAdvanceTimeUIComponent;
    @BindView(R.id.amb_ll_main)
    LinearLayout llMain;

    @BindView(R.id.amb_total_income)
    SelectCardUIComponent totalIncome;
    @BindView(R.id.amb_cash_advance_activity)
    SelectCardUIComponent cashAdvanceActivity;
    @BindView(R.id.amb_loan_activity)
    SelectCardUIComponent loanActivity;
    @BindView(R.id.amb_total_expenditure)
    SelectCardUIComponent totalExpenditure;
    @BindView(R.id.amb_roll_out)
    SelectCardUIComponent rollOut;
    @BindView(R.id.amb_empty_view)
    CashEmptyUIComponent cashEmptyUIComponent;

    private BillBean billBean;


    @Override
    protected void initView() {
        initTitleBar();
        initData();
        initListener();
    }

    private void initListener() {
        cashAdvanceTimeUIComponent.setOnSelectMonthClickListener(new SelectMonthPopupUtil.OnSelectMonthClickListener() {
            @Override
            public void getPosition(int position) {
                if (position < billBean.getMonth().size()) {
                    cashAdvanceTimeUIComponent.setTimeContent(billBean.getMonth().get(position));
                }
                if ((position + 1) <= billBean.getTrade().size() && null != billBean.getTrade().get(position)
                        && null != billBean.getTrade().get(position).getIncome_total()) {
                    cashEmptyUIComponent.setVisibility(View.GONE);
                    setBillContent(billBean.getTrade().get(position));
                    return;
                }
                cashEmptyUIComponent.initEmptyView("本月暂时没有任何记录！\n看看别的吧！");
            }
        });
    }

    private void setBillContent(BillBean.TradeBean tradeBean) {
        totalIncome.setTitleAndContentBold("总收入", FRCollection.appendAsString("+", NumberUtil.getTwoDecimalPlaces(tradeBean.getIncome_total()), ""));
        cashAdvanceActivity.setTitleAndContent("取现活动", FRCollection.appendAsString("+", NumberUtil.getTwoDecimalPlaces(tradeBean.getIncome_invitation()), ""));
        loanActivity.setTitleAndContent("贷款活动", FRCollection.appendAsString("+", NumberUtil.getTwoDecimalPlaces(tradeBean.getIncome_activity()), ""));
        totalExpenditure.setTitleAndContentBold("总支出", FRCollection.appendAsString("-", NumberUtil.getTwoDecimalPlaces(tradeBean.getOutcome_total()), ""));
        rollOut.setTitleAndContent("转出", FRCollection.appendAsString("-", NumberUtil.getTwoDecimalPlaces(tradeBean.getOutcome_total()), ""));
    }

    private void initData() {
        GetBillModel.getInstance().execute(new FRBaseObserver<BillBean>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(BillBean data) {
                if (null != data) {
                    llMain.setVisibility(View.VISIBLE);
                    billBean = data;
                    cashAdvanceTimeUIComponent.setMonthDataList(getSelectMonthList(data.getMonth()));
                    cashAdvanceTimeUIComponent.setTimeContent("");
                    if (null != data.getTrade() && data.getTrade().size() > 0 && null != data.getTrade().get(0)
                            && null != data.getTrade().get(0).getIncome_total()) {
                        cashEmptyUIComponent.setVisibility(View.GONE);
                        setBillContent(data.getTrade().get(0));
                    } else {
                        cashEmptyUIComponent.initEmptyView("本月暂时没有任何记录！\n看看别的吧！");
                    }
                }
            }
        });
    }

    private List<SelectMonthBean> getSelectMonthList(List<String> monthList) {
        List<SelectMonthBean> list = new ArrayList<>();
        SelectMonthBean data;
        for (int i = 0; i < monthList.size(); i++) {
            data = new SelectMonthBean();
            if (i == 0) {
                data.setSelected(true);
            }
            data.setTime(monthList.get(i));
            list.add(data);
        }
        return list;
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("账单");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cashAdvanceTimeUIComponent.setImageRes();
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_month_bill;
    }
}
